<?php

include "lib/start.php";

/*
 * controllo della presenza di $_SESSION['__current_year__'] (bug #93)
 */
if(!isset($_SESSION['__current_year__'])){
	header("Location: index.php?auto=1");
}

header("Content-type: text/plain");

$sel_user = "SELECT uid, nome, cognome, gruppi, username, accessi, permessi FROM utenti WHERE username = '".trim($_POST['nick']);
$sel_user .= "' AND password = '".trim($_POST['pass'])."'";
$_SESSION['sel'] = $sel_user;
try{
	$res_user = $db->executeQuery($sel_user);
} catch (MySQLException $ex){
	print("ko;".$ex->getMessage());
	exit;
}
if($res_user->num_rows < 1){
    print("ko;Nessun utente presente: $sel_user");
    exit;
}
$utente = $res_user->fetch_assoc();

$gid = split(",", $utente['gruppi']);
$user = new User($utente['uid'], $utente['nome'], $utente['cognome'], $gid, $utente['permessi']);

/**
 * profile
 */
$sel_profile = "SELECT * FROM profili WHERE id = ".$user->getUid();
try{
	$res_profile = $db->executeQuery($sel_profile);
} catch (MySQLException $ex){
	print("ko;".$ex->getMessage());
	exit;
}
if($res_profile->num_rows){
	$profile = $res_profile->fetch_assoc();
	$user->setProfile($profile);
}

/**
 * subjects and classes : only for teachers
 */
if($user->isTeacher()){
	$sel_subject = "SELECT materia FROM docenti WHERE id_docente = ".$user->getUid();
	try{
		$res_materia = $db->executeQuery($sel_subject);
	} catch (MySQLException $ex){
		print("ko;".$ex->getMessage());
		exit;
	}
	$materia = $res_materia->fetch_assoc();
	$user->setSubject($materia['materia']);

	/**
	 * populate the classes array
	 */
	$classes = array();
	$sel_cdc = "SELECT classi.id_classe, CONCAT(classi.anno_corso, classi.sezione) AS classe, id_materia, coordinatore FROM classi, cdc WHERE classi.id_classe = cdc.id_classe AND id_docente = ".$user->getUid()." AND id_anno = ".$_SESSION['__current_year__']->get_ID()." ORDER BY classi.sezione, classi.anno_corso";
	try{
		$res_cdc = $db->executeQuery($sel_cdc);
	} catch (MySQLException $ex){
		print("ko;".$ex->getMessage());
		exit;
	}
	while ($row = $res_cdc->fetch_assoc()){
		if(!isset($classes[$row['id_classe']])){
			//fwrite($log, "Array created\n");
			$classes[$row['id_classe']] = array();
			$classes[$row['id_classe']]['coordinatore'] = 0;
			$classes[$row['id_classe']]['materie'] = array();
			$classes[$row['id_classe']]['classe'] = $row['classe'];
		}
		if($row['coordinatore'] == 1){
			$classes[$row['id_classe']]['coordinatore'] = 1;
			//fwrite($log, "Coordinator ".$row['classe']."\n");
		}
		else{
			//fwrite($log, "No coordinator ".$row['classe']."==".$row['coordinatore']."\n");
		}
		
		array_push($classes[$row['id_classe']]['materie'], $row['id_materia']);
	}
	$user->setClasses($classes);
}
else{
	
}
$_SESSION['__user__'] = $user;
$_SESSION['__accessi__'] = $utente['accessi'] + 1;

$update = "UPDATE utenti SET accessi = (accessi + 1), previous_access = last_access, last_access = NOW() WHERE uid = ".$utente['uid'];
$upd = $db->executeUpdate($update);

print("0;".$utente['gruppi'].";".$utente['nome'].";".$utente['cognome'].";".$_SESSION['__accessi__'].";".$utente['permessi']);
exit;


?>